package preorder

import (
    "time"
	"strconv"
	//"errors"
	"dhfshop/controllers/base"
	"dhfshop/models/preorder"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/validation"
)

type TemplateController struct {
	base.BaseController
}

func (this *TemplateController) List() {
	query,_,fields,order_by,offset,limit,err := this.GetQueryPara()
	if err != nil {
		this.ServeError(err.Error())
		return
	}
	partnerId := int64(this.Ctx.Input.GetData("curCompanyId").(int))
	if value, ok := query["is_built_in"]; ok&&value == "1" {
	   //beego.Debug("is_built_in:", value)
	}else{
	   //beego.Debug("List partnerId:", partnerId)
	   query["partner_id"] = strconv.FormatInt(partnerId, 10)
	}
	list, cnt, err := preorder.GetAllTemplates(query, fields, order_by, offset, limit)
	if err != nil {
		this.ServeError(err.Error())
		return
	}
	rsp := struct {
		TotalCount int64                `json:"total_count"`
		Template   []*preorder.Template `json:"templates"`
	}{TotalCount: cnt, Template: list}
	this.Data["json"] = rsp
	this.ServeJSON()
}

func (this *TemplateController) PrivateTemp() {
	v := struct {
		PartnerId  int64    `form:"-"`
		StoreId    int64    `form:"store_id"`
		TemplateId int64    `form:"template_id"`
		Title      string   `form:"title"`
	}{}
	v.PartnerId = int64(this.Ctx.Input.GetData("curCompanyId").(int))
	beego.Debug("Post partnerId:", v.PartnerId)
	this.ParseForm(&v)
	valid := validation.Validation{}
	valid.Required(v.StoreId, "store_id")
	valid.Required(v.TemplateId, "template_id")
	valid.Required(v.Title, "title")
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key + " " + valid.Errors[0].Message)
		return
	}
	template, err := preorder.GetTemplateById(v.TemplateId)
	if err != nil {
		this.ServeError(err.Error())
		return
	}
	template.TemplateId = 0   //
	template.PartnerId = v.PartnerId
	template.StoreId = v.StoreId
	template.Title = v.Title
	template.IsBuiltIn = 0    //非内建
	_, err = preorder.AddTemplate(template)
	if err != nil {
		this.ServeError(err.Error())
		return
	}
	this.Data["json"] = template
	this.ServeJSON()
}

func (this *TemplateController) Update() {
	var v preorder.Template
	this.ParseForm(&v)
	valid := validation.Validation{}
	valid.Required(v.TemplateId, "template_id")
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key + " " + valid.Errors[0].Message)
		return
	}
	updateFields, err := this.GetUpdateFields(v, "", "form", []string{"period_stl_id"})
	if err != nil {
		this.ServeError(err.Error())
		return
	}
	if len(updateFields) > 0 {
		err := preorder.UpdateTemplateById(&v, updateFields...)
		if err != nil {
			this.ServeError(err.Error())
			return
		}
	}
	rsp := struct {
		TemplateId  int64     `json:"template_id"`
		Modified    time.Time `json:"modified"`
	}{TemplateId: v.TemplateId, Modified: time.Now().UTC()}
	this.Data["json"] = rsp
	this.ServeJSON()
}

func (this *TemplateController) Delete() {
	var v preorder.Template
	this.ParseForm(&v)
	valid := validation.Validation{}
	valid.Required(v.TemplateId, "template_id")
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key + " " + valid.Errors[0].Message)
		return
	}
	err := preorder.DeleteTemplate(v.TemplateId)
	if err != nil {
		this.ServeError(err.Error())
		return
	}
	r := struct {
		TemplateId  int64    `json:"template_id"`
		Modified  time.Time   `json:"modified"`
	}{TemplateId: v.TemplateId, Modified: time.Now().UTC()}
	this.Data["json"] = r
	this.ServeJSON()
}

